package com.panshi.controller;


import com.panshi.IZxCommentApi;
import com.panshi.config.MyControllerReturnUtls;
import com.panshi.domain.ZxComment;
import com.panshi.domain.vo.PageBean;
import com.panshi.domain.vo.ZxResult;
import com.panshi.service.ZxArticleService;
import com.panshi.service.ZxCommentService;
import com.panshi.vo.CodeMsgConstant;
import com.panshi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.stream.events.Comment;
import java.util.List;

/**
 * <p>
 * 前端控制器 㔿
 * </p>
 *
 * @author zhy
 * @since 2020-07-29
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "资讯评论")
@RequestMapping("/api/comment")
public class ZxCommentController implements IZxCommentApi {
    @Resource
    private ZxCommentService zxCommentService;
    @Resource
    private ZxArticleService zxArticleService;

    @PostMapping("/add")
    public ZxResult addComment(@RequestBody ZxComment zxComment){
       int  result = zxCommentService.addZxComment(zxComment);
        if(result>0){
            if(zxComment.getParentId()==0){
                zxArticleService.comment(zxComment.getAticleId());//增加资讯表中的评论数
            }
            return new ZxResult(200, "操作成功", zxCommentService.getCommentBayId(zxComment.getAticleId()));
        } else {
            return new ZxResult(400, "操作失败", null);
        }
    }

    @ApiOperation("资讯评论点赞")
    @PostMapping("/Raise")
    public ZxResult ZxCommentRaise(@RequestBody ZxComment zxComment) {
        int result=0;
        if(zxCommentService.queryUserId(zxComment)==0){
           result= zxCommentService.addCommentRaise(zxComment);
           zxCommentService.ZxCommentRaise(zxComment.getId());
            return MyControllerReturnUtls.ControllerReturn(result);
        }
        return MyControllerReturnUtls.ControllerReturn(result);
    }



    @ApiOperation("查询文章所有评论回复")
    @PostMapping("/commentReply")
    public ZxResult getCommentReply(@RequestBody PageBean pageBean) {
        List<ZxComment> list = zxCommentService.getCommentReply(pageBean);
        log.info("回复+++++{}",list);
        if (list.size() > 0) {
            return new ZxResult(200, "操作成功", list);
        } else {
            return new ZxResult(400, "操作失败", null);
        }
    }


    @ApiOperation("查询文章所有评论")
    @PostMapping("/getComment")
    public R getComment(@RequestBody PageBean pageBean) {
        List<ZxComment> list = zxCommentService.getCommentBayZxId(pageBean);
        int total = zxCommentService.getCommentReply(pageBean).size();
        log.info("评论{}",list);
        if (list.size() > 0) {
          return R.ok(CodeMsgConstant.CODE_0).data("date",list);
        } else {
          return R.error(CodeMsgConstant.CODE_1);
        }
    }
    @ApiOperation("查询文章评论详情")
    @PostMapping("/getComment/{id}")
    public ZxResult getCommentBayId(@PathVariable("id") Integer id) {
        ZxComment  comment = zxCommentService.getCommentBayId(id);
        log.info("评论{}",comment);
        if (comment!=null) {
            return new ZxResult(200, "操作成功", comment);
        } else {
            return new ZxResult(400, "操作失败", null);
        }
    }



}

